package org.ztr.yanai.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.ztr.yanai.blog.dto.PageRequestDTO;
import org.ztr.yanai.blog.dto.RegisterDTO;
import org.ztr.yanai.blog.dto.UserDTO;
import org.ztr.yanai.blog.entity.User;
import org.ztr.yanai.blog.enums.UserStatusEnum;
import org.ztr.yanai.blog.service.UserService;
import org.ztr.yanai.blog.utils.EntityDtoConverter;
import org.ztr.yanai.blog.utils.Result;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/11/22 10:00
 * @Description: 用户管理控制器，提供用户CRUD操作
 * @Version: 1.0
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户相关的 API 接口")
@Validated
public class UserController {

    private final UserService userService;

    // 构造器注入
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping
    @Operation(summary = "创建用户", description = "根据传入的用户信息创建新用户")
    public Result<UserDTO> createUser(@Valid @RequestBody RegisterDTO registerDTO) {
        User user = userService.register(registerDTO);
        return Result.success(EntityDtoConverter.toUserDTO(user));
    }

    @DeleteMapping("/{userId}")
    @Operation(summary = "删除用户", description = "根据用户 ID 删除用户")
    @Parameter(name = "userId", description = "用户 ID", required = true)
    public Result<Void> deleteUser(@PathVariable Long userId) {
        userService.deleteUserById(userId);
        return Result.success();
    }

    @GetMapping("/{userId}")
    @Operation(summary = "获取用户详情", description = "根据用户 ID 获取用户详细信息")
    @Parameter(name = "userId", description = "用户 ID", required = true)
    public Result<UserDTO> getUserById(@PathVariable Long userId) {
        User user = userService.getUserById(userId);
        return Result.success(EntityDtoConverter.toUserDTO(user));
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询用户", description = "支持分页和关键字查询用户列表")
    public Result<Page<UserDTO>> listUsersByPage(@Valid @ModelAttribute PageRequestDTO pageRequest) {
        Page<User> userPage = userService.listUsersByPage(pageRequest);

        // 转换为DTO
        Page<UserDTO> dtoPage = new Page<>(userPage.getCurrent(), userPage.getSize(), userPage.getTotal());
        List<UserDTO> dtoList = userPage.getRecords().stream()
                .map(EntityDtoConverter::toUserDTO)
                .collect(Collectors.toList());
        dtoPage.setRecords(dtoList);

        return Result.success(dtoPage);
    }

    @PutMapping
    @Operation(summary = "更新用户", description = "根据传入的用户信息更新用户数据")
    public Result<UserDTO> updateUser(@RequestBody User user) {
        User updatedUser = userService.updateUserInfo(user);
        return Result.success(EntityDtoConverter.toUserDTO(updatedUser));
    }

    @PostMapping("/batch/status")
    @Operation(summary = "批量更新用户状态", description = "根据用户 ID 列表批量更新用户状态")
    public Result<Void> batchUpdateStatus(
            @RequestBody List<Long> userIds,
            @RequestParam UserStatusEnum status) {
        userService.batchUpdateUserStatus(userIds, status);
        return Result.success();
    }
}